<div class="tmd-doc">
<h1 class="tmd-header-1">
Generics
</h1>
<p></p>
<div class="tmd-usual">
Before Go 1.18, Go only supported built-in generics. Since Go 1.18, Go also supports custom generics. This article only introduces built-in generics.
</div>
<p></p>
<div class="tmd-usual">
Go built-in generic types are supported through first-class citizen composite types. We can use composite types to create infinite custom types by using the composite types. This article will show some type composition examples and explain how to read these composited types.
</div>
<p></p>
<h3 class="tmd-header-3">
Type Composition Examples
</h3>
<p></p>
<div class="tmd-usual">
Type compositions in Go are designed very intuitive and easy to interpret. It is hard to get lost in understanding Go composite types, even if for some very complex ones. The following will list several type composition examples, from simpler ones to more complex ones.
</div>
<p></p>
<div class="tmd-usual">
Let's view an simple composite type literal.
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">[3][4]int
</code></pre>
<p></p>
<div class="tmd-usual">
When interpreting a composite type, we should look at it from left to right. The <code class="tmd-code-span">[3]</code> on the left in the above type literal indicates that this type is an array type. The whole right part following the <code class="tmd-code-span">[4]int</code> is another array type, which is the element type of the first array type. The element type of the element type (an array type) of the first array type is built-in type <code class="tmd-code-span">int</code>. The first array type can be viewed as a two-dimensional array type.
</div>
<p></p>
<div class="tmd-usual">
An example on using this two-dimensional array type.
</div>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

import (
	"fmt"
)

func main() {
	matrix := [3][4]int{
		{1, 0, 0, 1},
		{0, 1, 0, 1},
		{0, 0, 1, 1},
	}

	matrix[1][1] = 3
	a := matrix[1] // type of a is [4]int
	fmt.Println(a) // [0 3 0 1]
}
</code></pre>
<p></p>
<div class="tmd-usual">
Similarly,
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
<code class="tmd-code-span">[][]string</code> is a slice type whose element type is another slice type <code class="tmd-code-span">[]string</code>.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<code class="tmd-code-span">**bool</code> is a pointer type whose base type is another pointer type <code class="tmd-code-span">*bool</code>.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<code class="tmd-code-span">chan chan int</code> is a channel type whose element type is another channel type <code class="tmd-code-span">chan int</code>.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<code class="tmd-code-span">map[int]map[int]string</code> is a map type whose element type is another map type <code class="tmd-code-span">map[int]string</code>. The key types of the two map types are both <code class="tmd-code-span">int</code>.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<code class="tmd-code-span">func(int32) func(int32)</code> is a function type whose only return result type is another function type <code class="tmd-code-span">func(int32)</code>. The two function types both have only one input parameter with type <code class="tmd-code-span">int32</code>.
</div>
</li>
</ul>
<p></p>
<div class="tmd-usual">
Let's view another type.
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">chan *[16]byte
</code></pre>
<p></p>
<div class="tmd-usual">
The <code class="tmd-code-span">chan</code> keyword at the left most indicates this type is a channel type. The whole right part <code class="tmd-code-span">*[16]byte</code>, which is a pointer type, denotes the element type of this channel type. The base type of the pointer type is <code class="tmd-code-span">[16]byte</code>, which is an array type. The element type of the array type is <code class="tmd-code-span">byte</code>.
</div>
<p></p>
<div class="tmd-usual">
An example on using this channel type.
</div>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

import (
	"fmt"
	"time"
	"crypto/rand"
)

func main() {
	c := make(chan *[16]byte)

	go func() {
		// Use two arrays to avoid data races.
		var dataA, dataB = new([16]byte), new([16]byte)
		for {
			_, err := rand.Read(dataA[:])
			if err != nil {
				close(c)
			} else {
				c &lt;- dataA
				dataA, dataB = dataB, dataA
			}
		}
	}()

	for data := range c {
		fmt.Println((*data)[:])
		time.Sleep(time.Second / 2)
	}
}
</code></pre>
<p></p>
<div class="tmd-usual">
Similarly, type <code class="tmd-code-span">map[string][]func(int) int</code> is a map type. The key type of this map type is <code class="tmd-code-span">string</code>. The remaining right part <code class="tmd-code-span">[]func(int) int</code> denotes the element type of the map type. The <code class="tmd-code-span">[]</code> indicates the element type is a slice type, whose element type is a function type <code class="tmd-code-span">func(int) int</code>.
</div>
<p></p>
<div class="tmd-usual">
An example on using the just explained map type.
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

import "fmt"

func main() {
	addone := func(x int) int {return x + 1}
	square := func(x int) int {return x * x}
	double := func(x int) int {return x + x}

	transforms := map[string][]func(int) int {
		"inc,inc,inc": {addone, addone, addone},
		"sqr,inc,dbl": {square, addone, double},
		"dbl,sqr,sqr": {double, double, square},
	}

	for _, n := range []int{2, 3, 5, 7} {
		fmt.Println("&gt;&gt;&gt;", n)
		for name, transfers := range transforms {
			result := n
			for _, xfer := range transfers {
				result = xfer(result)
			}
			fmt.Printf(" %v: %v \n", name, result)
		}
	}
}
</code></pre>
<p></p>
<div class="tmd-usual">
Below is a type which looks some complex.
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">[]map[struct {
	a int
	b struct {
		x string
		y bool
	}
}]interface {
	Build([]byte, struct {x string; y bool}) error
	Update(dt float64)
	Destroy()
}
</code></pre>
<p></p>
<div class="tmd-usual">
Let's read it from left to right. The starting <code class="tmd-code-span">[]</code> at the left most indicates this type is a slice type. The following <code class="tmd-code-span">map</code> keyword shows the element type of the slice type is a map type. The struct type denoted by the struct literal enclosed in the <code class="tmd-code-span">[]</code> following the <code class="tmd-code-span">map</code> keyword is the key type of the map type. The element type of the map type is an interface type which specifies three methods. The key type, a struct type, has two fields, one field <code class="tmd-code-span">a</code> is of <code class="tmd-code-span">int</code> type, and the other field <code class="tmd-code-span">b</code> is of another struct type <code class="tmd-code-span">struct {x string; y bool}</code>.
</div>
<p></p>
<div class="tmd-usual">
Please note that the second struct type is also used as one parameter type of one method specified by the just mentioned interface type.
</div>
<p></p>
<div class="tmd-usual">
To get a better readability, we often decompose such a type into multiple type declarations. The type alias <code class="tmd-code-span">T</code> declared in the following code and the just explained type above denote the identical type.
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">type B = struct {
	x string
	y bool
}

type K = struct {
	a int
	b B
}

type E = interface {
	Build([]byte, B) error
	Update(dt float64)
	Destroy()
}

type T = []map[K]E
</code></pre>
<p></p>
<h3 class="tmd-header-3">
Built-in Generic Functions
</h3>
<p></p>
<div class="tmd-usual">
Besides the built-in generics for composite types, there are several built-in functions which also support generics. Such as the built-in <code class="tmd-code-span">len</code> function can be used to get the length of values of arrays, slices, maps, strings and channels. Generally, the functions in the <code class="tmd-code-span">unsafe</code> standard package are also viewed as built-in functions. The built-in generic functions have been introduced in previous articles,
</div>
<p></p>
<h3 class="tmd-header-3">
Custom Generics
</h3>
<p></p>
<div class="tmd-usual">
Since version 1.18, Go has already supported custom generics. Please read the <a href="https://go101.org/generics/101.html">Go Generics 101</a> book to get how to use custom generics.
</div>
<p></p>
<p></p>
</div>
